package org.NooLab.randomgraph;

import org.NooLab.randomgraph.components.TextChunkerFactory;
import org.NooLab.randomgraph.components.TextChunkerIntf;
import org.NooLab.randomgraph.components.TextCleanerIntf;
import org.NooLab.randomgraph.components.WordCleanerIntf;
import org.NooLab.structures.randomgraph.ParticledText;
import org.NooLab.structures.randomgraph.ParticledTextIntf;
import org.NooLab.utilities.CallbackForPrcCompletionIntf;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.StringsUtil;



/**
 * 
 * 
 * 
 *
 *
 */
public class BasicTextProcessor implements CallbackForPrcCompletionIntf{

	ParticledText  particledText ;
	
	RandomGraph randomGraph ;
	
	TextChunkerIntf wordchunker ;
	TextChunkerIntf sentencechunker ;
	
	
	String rawText;
	
	PrintLog out = new PrintLog(2,true) ;
	StringsUtil strgutil = new StringsUtil();           
	private double lastprcValue=0;

	WordCleanerIntf wordcleaner;
	TextCleanerIntf textCleaner ;
	
	// ========================================================================
	public BasicTextProcessor(RandomGraph rgraph) {
		
		randomGraph = rgraph ;
		particledText =  new ParticledText() ;
		
		wordcleaner = randomGraph.wordcleaner ;
		textCleaner = randomGraph.textCleaner ;
	}
	// ========================================================================

	
	@Override   // instantiated by [StringsUtil].setDisplayCompletion()
	public void setCompletionPercentage(double prcValue) {
		if ((prcValue-lastprcValue>0.1) || (prcValue<0.00001) || (prcValue>98.999)){
			try{
				String zs = String.format("%.1f", (double)(100.0*prcValue) )+"%";
				randomGraph.out.print(3, "string split "+zs);
			}catch(Exception e){
				e.printStackTrace() ;
			}
			lastprcValue  = prcValue;	
		} // > last?
		
	}


	public int digestText( int activeEntity ) {
		
		int r=-1;
		
		rawText = cleanText();
		
		particledText.setRawText( rawText );

		if (activeEntity == RandomGraphIntf._RG_ENTITY_WORD){
			
			wordchunker = TextChunkerFactory.getChunker( TextChunkerIntf._CHUNKING_TARGET_WORD );  
			
			wordchunker.plugParticledTextContainer( (ParticledTextIntf) particledText );
			
			if (particledText.getRawText().length()>15000){
				wordchunker.setCallback(this) ;
			}
			
			wordchunker.setWordcleaner(wordcleaner) ;
			wordchunker.setTextcleaner(textCleaner) ;
			
			/*
			 *  
			 */
											out.print(2, "chunking text into words (keeping the information about word order...");
			r = wordchunker.perform();
											out.print(2, "chunking text into words done.");
		}
		
		if (activeEntity == RandomGraphIntf._RG_ENTITY_SENTENCE ){
			
			sentencechunker = TextChunkerFactory.getChunker( TextChunkerIntf._CHUNKING_TARGET_WORD );
			sentencechunker.plugParticledTextContainer((ParticledTextIntf) particledText);
			
			sentencechunker.setCallback(this) ;
			
			r= sentencechunker.perform();
			
		}
		return r;
	}

	
	private String cleanText(){
		String text = rawText;
		
		strgutil.replaceAll(text, "  ", " ") ;
		
		// only simple stuff ...
		
		return text;
	}

	public void applyWordCleaner() {
		 
		
	}
	public void acquireText(String inText) {
		
		rawText = inText;
		
	}

	
	
	
	// ....................................................
	public RandomGraph getRandomGraph() {
		return randomGraph;
	}

	public void setRandomGraph(RandomGraph randomGraph) {
		this.randomGraph = randomGraph;
	}

	public ParticledText getParticledText() {
		return particledText;
	}



	 
}
